package com.kwxy.hyt.controller;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.kwxy.hyt.base.BaseController;
import com.kwxy.hyt.po.*;
import com.kwxy.hyt.utils.Pager;
import java.util.*;

import com.kwxy.hyt.po.*;
import com.kwxy.hyt.mapper.*;
import com.kwxy.hyt.service.*;

/**
 * @ClassName:  
 * @Description: 
 * @author  - - admin
 * @date - 2020年9月10日 09时48分24秒
 */


@Controller
@RequestMapping("/qj")
public class QjController extends BaseController {
	
	
	/**
	 * 依赖注入 start dao/service/===
	 */
	@Autowired
	private QjService qjService;
	
	@Autowired
	private StudentService studentService;
	
	@Autowired
	private FdyService fdyService;
	
	/**
	 * 分页查询 返回list对象(通过对By Sql)
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/findBySql")
	public String findBySql(Qj qj, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		Object attribute = request.getSession().getAttribute("userId");
		
		if (attribute == null){
			return "redirect:/login/uLogin";	
		}
		Integer userId = Integer.valueOf(attribute.toString());
		
		
		//分页查询
		String sql = "SELECT * FROM qj WHERE fdyId = "+userId;
        if(!isEmpty(qj.getBjId())){
        	sql += " and bjId like '%"+qj.getBjId()+"%'";
		}
        if(!isEmpty(qj.getStudentId())){
        	sql += " and studentId like '%"+qj.getStudentId()+"%'";
		}
        if(!isEmpty(qj.getFdyId())){
        	sql += " and fdyId like '%"+qj.getFdyId()+"%'";
		}
        if(!isEmpty(qj.getStartTime())){
        	sql += " and startTime like '%"+qj.getStartTime()+"%'";
		}
        if(!isEmpty(qj.getEndTime())){
        	sql += " and endTime like '%"+qj.getEndTime()+"%'";
		}
        if(!isEmpty(qj.getStatus())){
        	sql += " and status like '%"+qj.getStatus()+"%'";
		}
       sql += " ORDER BY ID DESC ";
		Pager<Qj> pagers = qjService.findBySqlRerturnEntity(sql);
		model.addAttribute("pagers", pagers);
		//存储查询条件
		model.addAttribute("obj", qj);
		return "qj/qj";
	}
	
	//findBySqlSign
	@RequestMapping(value = "/findBySqlSign")
	public String findBySqlSign(Qj qj, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		//分页查询
		String sql = "SELECT * FROM qj WHERE bjId = "+qj.getBjId();
        if(!isEmpty(qj.getBjId())){
        	sql += " and bjId like '%"+qj.getBjId()+"%'";
		}
        if(!isEmpty(qj.getStudentId())){
        	sql += " and studentId like '%"+qj.getStudentId()+"%'";
		}
        if(!isEmpty(qj.getFdyId())){
        	sql += " and fdyId like '%"+qj.getFdyId()+"%'";
		}
        if(!isEmpty(qj.getStartTime())){
        	sql += " and startTime like '%"+qj.getStartTime()+"%'";
		}
        if(!isEmpty(qj.getEndTime())){
        	sql += " and endTime like '%"+qj.getEndTime()+"%'";
		}
        if(!isEmpty(qj.getStatus())){
        	sql += " and status like '%"+qj.getStatus()+"%'";
		}
       sql += " ORDER BY ID DESC ";
		Pager<Qj> pagers = qjService.findBySqlRerturnEntity(sql);
		model.addAttribute("pagers", pagers);
		//存储查询条件
		model.addAttribute("obj", qj);
		model.addAttribute("bjId", qj.getBjId());
		return "qj/qjSign";
	}
	
	
	/**
	 * 我的请假
	 * @param qj
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	//myqj
	//findBySqlSign
	@RequestMapping(value = "/myqj")
	public String myqj(Qj qj, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		//分页查询
		
		Object attribute = request.getSession().getAttribute("userId");
		
		if (attribute == null){
			return "redirect:/login/uLogin";	
		}
		Integer userId = Integer.valueOf(attribute.toString());
		
		String sql = "SELECT * FROM qj WHERE studentId = "+userId;
		
        if(!isEmpty(qj.getBjId())){
        	sql += " and bjId like '%"+qj.getBjId()+"%'";
		}
        if(!isEmpty(qj.getStudentId())){
        	sql += " and studentId like '%"+qj.getStudentId()+"%'";
		}
        if(!isEmpty(qj.getFdyId())){
        	sql += " and fdyId like '%"+qj.getFdyId()+"%'";
		}
        if(!isEmpty(qj.getStartTime())){
        	sql += " and startTime like '%"+qj.getStartTime()+"%'";
		}
        if(!isEmpty(qj.getEndTime())){
        	sql += " and endTime like '%"+qj.getEndTime()+"%'";
		}
        if(!isEmpty(qj.getStatus())){
        	sql += " and status like '%"+qj.getStatus()+"%'";
		}
       sql += " ORDER BY ID DESC ";
		Pager<Qj> pagers = qjService.findBySqlRerturnEntity(sql);
		model.addAttribute("pagers", pagers);
		//存储查询条件
		model.addAttribute("obj", qj);
		return "qj/myqj";
	}
	
	/**
	 * 分页查询 返回list对象(通过Map)
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/findByMap")
	public String findByMap(Qj qj, Model model, HttpServletRequest request, HttpServletResponse response) {
		//通过map查询
		Map<String,Object> params = new HashMap<String,Object>();
        if(!isEmpty(qj.getBjId())){
        	params.put("bjId", qj.getBjId());
		}
        if(!isEmpty(qj.getStudentId())){
        	params.put("studentId", qj.getStudentId());
		}
        if(!isEmpty(qj.getFdyId())){
        	params.put("fdyId", qj.getFdyId());
		}
        if(!isEmpty(qj.getStartTime())){
        	params.put("startTime", qj.getStartTime());
		}
        if(!isEmpty(qj.getEndTime())){
        	params.put("endTime", qj.getEndTime());
		}
        if(!isEmpty(qj.getStatus())){
        	params.put("status", qj.getStatus());
		}
		//分页查询
		Pager<Qj> pagers = qjService.findByMap(params);
		model.addAttribute("pagers", pagers);
		//存储查询条件
		model.addAttribute("obj", qj);
		return "qj/qj";
	}
	
	/**********************************【增删改】******************************************************/
	
	/**
	 * 跳至添加页面
	 * @return
	 */
	@RequestMapping(value = "/add")
	public String add() {
		return "qj/add";
	}
	
	@RequestMapping(value = "/myadd")
	public String myadd() {
		return "qj/myadd";
	}

	/**
	 * 跳至详情页面
	 * @return
	 */
	@RequestMapping(value = "/view")
	public String view(Integer id,Model model) {
		Qj obj = qjService.load(id);
		model.addAttribute("obj",obj);
		return "qj/view";
	}
	
	/**
	 * 添加执行
	 * @return
	 */
	@RequestMapping(value = "/exAdd")
	public String exAdd(Qj qj, Model model, HttpServletRequest request, HttpServletResponse response) {
		
		Object attribute = request.getSession().getAttribute("userId");
		
		if (attribute == null){
			return "redirect:/login/uLogin";	
		}
		Integer userId = Integer.valueOf(attribute.toString());
		qj.setStatus(0);
		
		Student byId = studentService.getById(userId);
		Integer bjId = byId.getBjId();
		Fdy f = new Fdy();
		f.setBjId(bjId);
		List<Fdy> listAllByEntity = fdyService.listAllByEntity(f);
		
		qj.setBjId(bjId);
		qj.setFdyId(listAllByEntity.get(0).getId());
		qj.setStudentId(userId);
		
		qjService.insert(qj);
		return "redirect:/qj/myqj.action";
	}
	
	
	/**
	 * 跳至修改页面
	 * @return
	 */
	@RequestMapping(value = "/update")
	public String update(Integer id,Model model) {
		Qj obj = qjService.load(id);
		model.addAttribute("obj",obj);
		return "qj/update";
	}
	
	/**
	 * 添加修改
	 * @return
	 */
	@RequestMapping(value = "/exUpdate")
	public String exUpdate(Qj qj, Model model, HttpServletRequest request, HttpServletResponse response) {
		//1.通过实体类修改，可以多传修改条件
		qjService.updateById(qj);
		//2.通过主键id修改
		//qjService.updateById(qj);
		return "redirect:/qj/myqj.action";
	}
	
	@RequestMapping(value = "/sp")
	public String sp(Qj qj, Model model, HttpServletRequest request, HttpServletResponse response) {
		//1.通过实体类修改，可以多传修改条件
		Qj byId = qjService.getById(qj.getId());
		byId.setStatus(qj.getStatus());
		byId.setContent(qj.getContent());
		qjService.updateById(byId);
		return "redirect:/qj/findBySql.action";
	}
	
	/**
	 * 删除通过主键
	 * @return
	 */
	@RequestMapping(value = "/delete")
	public String delete(Integer id, Model model, HttpServletRequest request, HttpServletResponse response) {
		///1.通过主键删除
		qjService.deleteById(id);
		return "redirect:/qj/myqj.action";
	}
	
	
}
